package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import utils.Images;
import utils.Utils;

import main.Constants;
import main.Controller;
import main.Enemies;
import main.HP;
import main.Main;

public class GUI extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3929184124959510964L;
	private static GUI oneGUI = null;
	private static JButton start, stop, refreshHealth;
	private static Main bot = null;
	private static HP hp = null;
	private static Controller conductor = null;
	
	public static JLabel currentHP;
	public static JTextArea textWindow;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});

	}

	private static void createAndShowGUI() {

		JFrame mainFrame = new JFrame("MyFirst BOt");
		mainFrame.setAlwaysOnTop(true);
		mainFrame.setSize(250, 500);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		GUI gui = GUI.getInstance();

		mainFrame.add(gui);
		// mainFrame.pack();
		mainFrame.setVisible(true);
		

	}

	private static GUI getInstance() {
		if (oneGUI != null)
			return oneGUI;
		else
			return oneGUI = new GUI();
	}

	private GUI() {
		setOpaque(true);

		stop = new JButton();
		stop.setText("STOP BOt");
		stop.setEnabled(false);
		stop.setMnemonic(KeyEvent.VK_W);
		stop.addActionListener(this);

		add(stop);

		start = new JButton();
		start.setText("START BOt");
		start.setMnemonic(KeyEvent.VK_F);
		start.addActionListener(this);

		add(start);
		
		refreshHealth = new JButton();
		refreshHealth.setText("refreshHealth");
		refreshHealth.addActionListener(this);

		add(refreshHealth);
		
		currentHP = new JLabel();
		currentHP.setText("STUB");
		add(currentHP);
		
		textWindow = new JTextArea(20, 18);
		textWindow.setText("Stub");
		textWindow.setEditable(false);
		textWindow.setLineWrap(true);
		textWindow.setWrapStyleWord(true);
		
		
		add(textWindow);

	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("START BOt")) {
			bot = new Main(Enemies.grol8 , 2090);
			bot.start();
			conductor = new Controller();
			conductor.start();
			start.setEnabled(false);
			stop.setEnabled(true);
		}
		if (e.getActionCommand().equals("STOP BOt")) {
			bot.interrupt();
			conductor.interrupt();
			start.setEnabled(true);
			stop.setEnabled(false);
		}
		if (e.getActionCommand().equals("refreshHealth")) {
			try {
				Images.imageFullHealth = Utils.createSnapshot(Constants.HEALTH, Images.HEALTH_NAME);
				Images.imageLongFullHealth = Utils.createSnapshot(Constants.LONG_HEALTH, Images.LONG_HEALTH_NAME);
				if(hp != null) hp.stop();
				hp = new HP();
				hp.start();
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	
	
	
}
